#include <iostream>
#include <pthread.h>
#include <string>
using namespace std;
class Thread
{
public:
    typedef void (*func_t)(void *);
    enum Status
    {
        NEW = 0,
        RUNNING,
        EXIT
    };
    Thread(int num, func_t func, void *arg)
    {
        char name[64];
        snprintf(name, sizeof(name), "thread-%d", num);
        _name = name;
        _status = NEW;
        _tid = 0;
        _func = func;
        _arg = arg;
    }

    string &name()
    {
        return _name;
    }

    pthread_t ThreadTid()
    {
        if (_status == RUNNING)
        {
            return _tid;
        }
        else
        {
            return 0;
        }
    }

    static void *RunHelper(void *arg)
    {
        Thread *td = static_cast<Thread *>(arg);
        (*td)();
    }
    void operator()()
    {
        if (_func != nullptr)
        {
            _func(_arg);
        }
    }

    void Run()
    {
        int n;
        n = pthread_create(&_tid, nullptr, RunHelper, this);
        if (n != 0)
        {
            exit(1);
        }
        _status = RUNNING;
    }

    void join()
    {
        int n=pthread_join(_tid,nullptr);
        if(n!=0)
        {
            exit(2);
        }
        _status=EXIT;
    }


    int ThreadStatus()
    {
        return _status;
    }


private:
    pthread_t _tid;
    string _name;
    enum Status _status;
    func_t _func;
    void *_arg;
};